/**
 * 
 */
package cn.biplam.front.component.view.mdManage.buffer2md.main;

import cn.biplam.common.bean.md.ModelImportTaskObj;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.BufCompTabCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.view.mdManage.buffer2md.main.callback.Buffer2MdDealCallBack;
import cn.biplam.front.component.view.mdManage.buffer2md.main.callback.Buffer2MdEstTableNameCallBack;
import cn.biplam.front.component.view.mdManage.buffer2md.main.callback.Buffer2MdImpTableNameCallBack;
import cn.biplam.front.component.view.mdManage.buffer2md.main.callback.Buffer2MdOperatorCallBack;
import cn.biplam.front.component.view.mdManage.buffer2md.main.callback.Buffer2MdThemeCallBack;
import cn.biplam.front.component.view.systemManage.task.TaskProcessMain;
import cn.biplam.front.component.view.systemManage.task.modelimport.ModelImportTaskManage;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.AjxScript.Level;
import cn.biplam.front.htmlscript.script.AjxScript.Popup_Name;
import cn.biplam.front.template.base.HiddenParams;
import cn.biplam.front.template.base.Text;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.pageFrame.mdManage.MdOperate;

/**
 * 	buffer2md表比较管理页面
 * @author 	WangSong 2009-5-15 下午12:02:43
 * @version 1.0
 */
public class Buffer2MdCompareTable extends AjxResponse{

	public final static String cId="2202";
	public final static String aId="220201";
	
	/** 每页显示行数*/
	private int showPageLine=15;
	/** 页面宽度*/
	private String width="780";
	private String modelType;
	private String themeId;

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		/**这里的recordId是任务id*/
		return get(recordId,mUser,sHomeUrl);
	}

	/**
	 * 缓冲区-MD增量更新比较页面
	 * @throws ErrorMessage
	 */
	public HtmlWindow get(String recordId,UserInfo mUser,String sHomeUrl) throws ErrorMessage{
		this.recordId = recordId;
		this.mUser = mUser;
		this.sHomeUrl = sHomeUrl;

		//创建页面模板
		MdOperate mmp = new MdOperate(cId,sHomeUrl,mUser,Popup_Name.Second.toString(),null);
		mmp.setRecordId(recordId);//设置主键ID
		//创建页面级按钮
		mmp.putButton("291", AjxScript.getSpNoRefresh(cId, //提交容器ID
				ModelImportTaskManage.aId_Import_TO_Md, //提交动作ID
				recordId                              //主键ID
				));

		//创建页面表格
		mmp.putDomain(getManage());
		mmp.putDomain(getParams());
		
		HtmlWindow win = mmp.get();
		return win;
	}

	/**
	 * 生成比较页面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow getManage() throws ErrorMessage{
		getThemeAndModelType(recordId);
		HtmlWindow manage = new HtmlDiv();
		getTable(manage);
		return manage;
	}

	/**
	 * 表信息
	 * @param manage
	 * @throws ErrorMessage
	 */
	private void getTable(HtmlWindow manage) throws ErrorMessage{
		DataFilter filter=new DataFilter();
		filter.put(MdStruc.ImpCompTabStruc.TASK_ID,recordId);
		OperatorResult sr=Operator.getPage(BufCompTabCode.Get_By_Task_ID, 
				mUser.getUserID(), filter,showPageLine);
		if(sr!=null && !sr.isNull()){

			/** 表主键 */
			String mainKey=MdStruc.ImpCompTabStruc.CTID;
			/** 表格列对应字段名 */
			String[] tableFields={MdStruc.ImpCompTabStruc.ORID,
					MdStruc.ImpCompTabStruc.IMP_TABLE_NAME,
					MdStruc.ImpCompTabStruc.OPERATOR_TYPE,
					MdStruc.ImpCompTabStruc.EST_TABLE_NAME,
					MdStruc.ImpCompTabStruc.THEME,
			"operator"};
			/** 表格列类型 */
			TablePropertys.Col_Content_Type[] fieldsType={
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.DESC_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.BUTTON
			};
			/** 设置列标题 */
			String[] tableColTitles={"序号","新导入表","处理","现有主题表","所属主题","操作"};
			TablePropertys property=new TablePropertys(tableFields,fieldsType,tableColTitles,sHomeUrl,mUser);

			/** 设置新导入表字段*/
			this.setImpTableName(property);
			/** 设置现有表字段绑定器*/
			this.setEstTableName(property);
			/** 设置操作类型*/
			this.setDeal(property);
			/** 设置主题字段操作*/
			this.setTheme(property);
			/** 设置操作按钮*/
			this.setOperator(property);

			/** 创建表对象 */
			FlowTable st=new FlowTable(cId+"table",mainKey,property); 
			st.setPageProperty(aId,recordId,cId);
			/** 将数据集放入表格 */
			st.put(sr);
			st.setWidth(width);
			manage.put(st.get());
		}else{
			HtmlWindow promt = new Text("比较表为空").get();
			promt.setStyleWidth(width);
			manage.put(promt);
		}
	}

	/**
	 * 设置新导入表字段绑定器
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setImpTableName(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME, new Buffer2MdImpTableNameCallBack());
		property.setColAction(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME,null,null,null,null);
		property.setColAction(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME, Window_Event.OnClick,
				TablePropertys.Action_Type.POP_SUBMIT_PV,BufferTableDetail.aId, null);
	}

	/**
	 * 设置现有表字段绑定器
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setEstTableName(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.ImpCompTabStruc.EST_TABLE_NAME, new Buffer2MdEstTableNameCallBack());
		property.setColAction(MdStruc.ImpCompTabStruc.EST_TABLE_NAME,null,null,null,null);
		property.setColAction(MdStruc.ImpCompTabStruc.EST_TABLE_NAME, Window_Event.OnClick,
				TablePropertys.Action_Type.POP_SUBMIT_PV,ModelTableDetail.aId, null);
	}

	/**
	 * 设置处理字段绑定器
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setDeal(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.ImpCompTabStruc.OPERATOR_TYPE, new Buffer2MdDealCallBack());
	}



	/**
	 * 设置主题字段操作
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setTheme(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.ImpCompTabStruc.THEME, new Buffer2MdThemeCallBack());
		property.setColAction(MdStruc.ImpCompTabStruc.THEME,null,null,null,null);
		property.setColAction(MdStruc.ImpCompTabStruc.THEME, 
				Window_Event.OnClick,
				TablePropertys.Action_Type.POP_SUBMIT_PR,
				Buffer2MdThemeModify.aId, 
				null,
				HiddenParams.hiddenId+cId);
	}

	/**
	 * 设置操作按钮
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setOperator(TablePropertys property) throws ErrorMessage{
		/**设置操作按钮*/
		property.setCallBack("operator", new Buffer2MdOperatorCallBack());
		/** 设置动作 略过、更新、修改、删除*/
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.SP,
				Buffer2MdTableOpt.aId_ignore,null,"taskId,"+recordId);
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.SP,
				Buffer2MdTableOpt.aId_update,null,"taskId,"+recordId);
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.SP,
				Buffer2MdTableOpt.aId_delete,null,"taskId,"+recordId);
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.POP_SUBMIT_P,Buffer2MdCompareTableCol.aId,Level.Third.toString());

	}

	/**
	 * 根据任务id取表的主题和模型对象类型
	 * @throws ErrorMessage 
	 */
	private void getThemeAndModelType(String taskId) throws ErrorMessage{
		ModelImportTaskObj mito = ModelImportTaskObj.getNewInstance(mUser,taskId);
		this.modelType = mito.getModelType();
		this.themeId = mito.getModelTheme();
	}

	/**
	 * 返回创建好的隐藏参数容器
	 * @return HtmlWindow
	 * @throws ErrorMessage
	 */
	private HtmlWindow getParams() throws ErrorMessage{
		HiddenParams hp = new HiddenParams(cId);
		hp.put("modelType", modelType);
		hp.put("themeId",this.themeId);
		hp.put("taskId",this.recordId);
		hp.put(TaskProcessMain.refreshPararentCId, ModelImportTaskManage.cId);
		hp.put(TaskProcessMain.refreshPararentAid, ModelImportTaskManage.aId_Main_Page);
		return hp.get();
	}
}
